
package com.android.nearshop.app.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.nearshop.R;
import com.android.nearshop.app.adapter.listadapter.Text3ListAdapter.Text3Item;

/*******************************************************
 * @author: zhaohua
 * @version: 2012-8-13
 * @see: list_item_dottext3 的 view
 * @Copyright: copyrights reserved by personal 2007-2011
*******************************************************/
public class Text3ItemView extends LinearLayout implements OnCheckedChangeListener
{
    private Text3Item mData;
	private TextView name;
	private ImageView iv_dot;
	private ImageView iv_expand;
	private CheckBox iv_check;
	
	public Text3ItemView(Context context)
	{
		super(context);
		initViews();
	}
	
	public Text3ItemView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		initViews();
	}
	
	private void initViews()
	{
		View row = inflate(getContext(), R.layout.list_item_text3, null);
		addView(row,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
		
		name = (TextView)findViewById(R.id.name);
		iv_expand = (ImageView)findViewById(R.id.iv_expand);
		iv_dot = (ImageView)findViewById(R.id.iv_dot);
		iv_check = (CheckBox)findViewById(R.id.iv_check);
	}
	
	public void bindData(Text3Item data)
	{
	    bindData(data,null);
	}

	public void bindData(Text3Item data,Boolean expand)
	{
	    if(data == null) return;
	    mData = data;
	    name.setText(data.get_name());
	    
	    iv_dot.setVisibility(data.isDotOpen(true, false) ? View.VISIBLE: View.GONE);
	    if(expand != null)
	    {
	        iv_dot.setVisibility(View.VISIBLE);
	        iv_dot.setImageResource(expand ? R.drawable.indicator_close : R.drawable.indicator_open);
	    }
	    if(iv_dot.getVisibility() == View.GONE)
	    {
	        name.setPadding(20, 0, 0, 0);
	    }
	    if(!data.isDotOpen(false, true))
	    {
	        iv_expand.setVisibility(View.GONE);
	        if(data.checked(true,false,false))
	        {
	            iv_check.setVisibility(View.VISIBLE);
	            iv_check.setChecked(data.checked(false,true,false));
	            iv_check.setOnCheckedChangeListener(this);
	        }else
	        {
	            iv_check.setVisibility(View.GONE);
	        }
	    }else
	    {
	        iv_check.setVisibility(View.GONE);
	        iv_expand.setVisibility(View.VISIBLE);
	    }
	}

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        boolean r = mData.checked(false, false, isChecked);
        if(r && mOnCheckedChangeListener != null)
        {
            mOnCheckedChangeListener.onCheckedChanged(mData, isChecked);
        }
    }
    //////////////////////////////////////////////////////////////////////////////
    private OnMCheckedChangeListener mOnCheckedChangeListener;
    public static interface  OnMCheckedChangeListener
    {
        public void onCheckedChanged(Text3Item data, boolean isChecked);
    }
    public void setOnMCheckedChangeListener(OnMCheckedChangeListener listener)
    {
        this.mOnCheckedChangeListener = listener;
    }
}
